<?php
    include('./config.php');
    include('./request.php');
    $goodsname = $result->goods_name;
    $goodprice = $result->goods_price;
    $goodsnums = $result->goods_nums;
    $goodstype = $result->goods_type;
    $marketname = $result->marketname;
    $goodsmodel = $result->goods_model;
    try{
        $pdo = new PDO($dsn,$user,$pass);
        $pdo->beginTransaction();
        $sql = "select * from goods where goods_name='".$goodsname."'";
        $res = $pdo->prepare($sql);
        $res->execute();
        $rea = $res->fetch(PDO::FETCH_ASSOC);
        if($rea['goods_name']==$goodsname){
            $goods_id = $rea['goods_id'];
            $sql = "update goods set goods_num=goods_num+".$goodsnums." where goods_id=".$goods_id;
            $res = $pdo->prepare($sql);
            $res->execute();
            $sql1 = "select * from goodsmodel where goods_model='".$goodsmodel."' and goods_id=".$goods_id;
            $res1 = $pdo->prepare($sql1);
            $res1->execute();
            $rea1 = $res1->fetch(PDO::FETCH_ASSOC);
            if($rea1['goods_model']==$goodsmodel && $rea1['goods_id']==$goods_id){
                $goodsAllnum = $rea1['goods_num']+$goodsnums;
                echo $goodsAllnum;
                $sql2 = "update goodsmodel set goods_num=".$goodsAllnum." where goods_model='".$goodsmodel."' and goods_id=".$goods_id;
                $res2 = $pdo->prepare($sql2);
                $res2->execute();
                if($res2){
                    $pdo->commit();
                    echo "ok";
                }else{
                    $pdo->rollBack();
                    echo "error";
                }    
            }
            else{
                $sql3 = "insert into goodsmodel values(".$goods_id.",'".$goodsmodel."',".$goodsnums.")";
                $res3 = $pdo->prepare($sql3);
                $res3->execute();
                if($res3){
                    $pdo->commit();
                    echo "ok";
                }else{
                    $pdo->rollBack();
                    echo "error";
                }
            }
        }else{
            $sql2 = "insert into goods(goods_name,goods_price,goods_num,goods_type,market_name)values('".$goodsname."',".$goodprice.",".$goodsnums.",'".$goodstype."','".$marketname."')";
            $res1 = $pdo->prepare($sql2);
            $res1->execute();
            if($res1){
                $sql2 = "select * from goods where goods_name='".$goodsname."'";    
                $res1 = $pdo->prepare($sql2);
                $res1->execute();
                $rea2 = $res1->fetch(PDO::FETCH_ASSOC);
                if($rea2['goods_name']==$goodsname){
                    $goodsid2 = $rea2['goods_id'];
                    $sql2 = "insert into goodsmodel values(".$goodsid2.",'".$goodsmodel."',".$goodsnums.")";
                    $res1 = $pdo->prepare($sql2);
                    $res1->execute();
                    if($res1){
                        $pdo->commit();
                        echo "ok";
                    }else{
                        $pdo->rollBack();
                        echo "error";
                    }
                }else{
                    $pdo->rollBack();
                    echo "error";
                }
            }else{
                $pdo->rollBack();
                echo "error";
            }
        }
        
    }catch(PDOException $e){
        $pdo->rollBack();
        echo $e->getMessage();
        echo "error";
    }
?>